package cn.edu.nju.software.cdiy.util;

public class NumberFormatter {
	public static double getNumber(String s) {
		//心如12CM 取出12
		char charArray[] = s.toCharArray();
		StringBuffer numBuffer = new StringBuffer();
		for (int i = 0; i < charArray.length; i++) {
			if (Character.isLetter(charArray[i]))
				break;
			else
				numBuffer.append(charArray[i]);
		}
		String s1 = numBuffer.toString();
		return Double.parseDouble(s1);
	}

	public static int[] getPriceArray(String string) {
		//形如23-34 取出23和34
		char charArray[] = string.toCharArray();
		int price[] = new int[2];
		price[0] = 0;
		price[1] = 0;
		StringBuffer sb = new StringBuffer();
		// 连续非数字
		int notNum = 0;
		for (int i = 0; i < charArray.length; i++) {
			if (Character.isDigit(charArray[i])) {
				notNum = 0;
				sb.append(charArray[i]);
			} else {
				notNum++;
				if (notNum == 2) {
					break;
				}
				if (notNum == 1) {
					if (price[0] == 0) {
						price[0] = Integer.parseInt(sb.toString());
						sb = new StringBuffer();
					} else {
						price[1] = Integer.parseInt(sb.toString());
						break;
					}
				}

			}
		}

		return price;
	}
	
	public static double[] getLengthAndWidth(String s){
		//形如23×45cm 取出23和45
		int i = s.lastIndexOf("c");
		s=s.substring(0, i);
		String[] s1 = s.split("×");
		double length = Double.parseDouble(s1[0]);
		double width = Double.parseDouble(s1[1]);

		
		double[] result = new double[2];
		result[0]=length;
		result[1]= width;
		
		return result;
	}
}
